﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using HarrisDesigns.Controls.dasBlogControls.Twitter;

namespace HarrisDesigns.Controls.dasBlogControls
{
    [DefaultProperty("TwitterAccount")]
    [ToolboxData(@"<{0}:TwitterStatusControl TwitterAccount=""yourtwitterid"" tweetCount=""10"" runat=""server""></{0}:TwitterStatusControl>")]
    public class TwitterStatusControl : Panel
    {
        private const string PollingMinutesViewStateIdentifier = "PollingMinutes";
        private const string TweetCountViewStateIdentifier = "TweetCount";
        private const string TwitterAccountViewStateIdentifier = "User";
        private const string TwitterStatusCssClass = "TwitterStatusItem";
        private const string TwitterStatusTimestampCssClass = "TwitterStatusTimestamp";

        public TwitterStatusControl()
        {
            Load += ControlLoad;
        }

        [Category("Behavior")]
        [DefaultValue("")]
        public string TwitterAccount
        {
            get
            {
                var s = (String) ViewState[TwitterAccountViewStateIdentifier];
                return (s ?? String.Empty);
            }

            set { ViewState[TwitterAccountViewStateIdentifier] = value; }
        }

        [Category("Appearance")]
        [DefaultValue("10")]
        public int TweetCount
        {
            get
            {
                object b = ViewState[TweetCountViewStateIdentifier];
                return ((b is int) ? (int) b : 10);
            }

            set { ViewState[TweetCountViewStateIdentifier] = value; }
        }

        [Category("Behavior")]
        [DefaultValue("5")]
        public int PollingMinutes
        {
            get
            {
                object b = ViewState[PollingMinutesViewStateIdentifier];
                return ((b is int) ? (int) b : 5);
            }

            set { ViewState[PollingMinutesViewStateIdentifier] = value; }
        }

        private void ControlLoad(object sender, EventArgs e)
        {
            List<Status> data = (new Repository{UserIdentity = TwitterAccount, MaxItems = TweetCount, PollingInterval = PollingMinutes}).GetCurrentData();
            foreach (Status status in data)
            {
                var statusPanel = new Panel();
                Controls.Add(statusPanel);
                statusPanel.CssClass = TwitterStatusCssClass;

                var statusText = new Literal();
                statusPanel.Controls.Add(statusText);
                statusText.Text = status.GetTextAsHtml() + " ";

                var statusDateDiff = new Label();
                statusPanel.Controls.Add(statusDateDiff);
                statusDateDiff.CssClass = TwitterStatusTimestampCssClass;

                var statusDateLink = new HyperLink();
                statusDateDiff.Controls.Add(statusDateLink);
                statusDateLink.Text = status.GetTimeDifference();
                statusDateLink.NavigateUrl = String.Format(@"http://twitter.com/{0}/statuses/{1}", TwitterAccount,
                                                           status.ID);
            }
        }
    }
}